package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.controller.form.AccountRegisterForm;
import com.woniuxy.model.VoteAccount;
import com.woniuxy.service.VoteAccountService;
import com.woniuxy.util.Result;
import com.woniuxy.util.exception.WoniuxyException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhouzhou
 * @since 2022-06-23
 */
@RestController
public class VoteAccountController {

    @Resource
    private VoteAccountService voteAccountService;

    @RequestMapping("/loginFail")
    public Result loginFail(){
        return Result.fail(403,"登陆失败",null);
    }

    @RequestMapping("/register")
    public Result addAccount(AccountRegisterForm accountRegisterForm) throws WoniuxyException {
        VoteAccount checkExist = null;
        System.out.println(accountRegisterForm);
        try {
            checkExist = voteAccountService.getOne(new QueryWrapper<VoteAccount>()
                    .eq("account_name", accountRegisterForm.getUsername()));
        }
        catch (Exception e){
            throw new WoniuxyException(10000,e.getMessage());
        }
        if (checkExist != null){
            return Result.fail(500,"用户已存在",null);
        }

        voteAccountService.save(new VoteAccount(null,accountRegisterForm.getUsername(),
                new BCryptPasswordEncoder().encode(accountRegisterForm.getPassword())));

        return Result.success(200,"注册成功",null);
    }
}

